<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
use common\models\Category;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Categories');
$this->params['breadcrumbs'][] = $this->title;

?>
<hr/>
<div class="category-index">

    <p>
        <?= Html::a(Yii::t('app', '新增分类'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
//			['class' => \yii\grid\CheckboxColumn::className()],

			'id',
			[
				'header' => '<a href="javascript:void()">分类名称</a>',
				'content' => function ($model) {
					return $model->name;
				}
			],
			[
				'header' => '<a href="javascript:void()">上级分类</a>',
				'content' => function ($model) {
					if($model->pid == 0) return '顶级分类';
					$category = new Category();
					return $category->getPname($model->pid);
				}
			],
			[
				'header' => '<a href="javascript:void()">子分类</a>',
				'content' => function ($model) {
					$category = new Category();
					$count = $category->find()->where(['pid' => $model->id])->count();
					if ($count > 0) {
						return Html::a($count, ['index', 'pid' => $model->id]);
					} else {
						return Html::a($count, 'javascript:');
					}

				}
			],

            'sort',
             'create_time:datetime',
			/*[
				'header' => '<a href="javascript:void()">状态</a>',
				'content' => function ($model) {
					return $model->status ? Html::button('正常', ['class' => 'btn btn-success btn-xs']) : Html::button('禁用', ['class' => 'btn btn-danger btn-xs']);
				}
			],*/

			[
				'class' => 'yii\grid\ActionColumn',
				'header' => '<a href="javascript:void()">操作</a>',
				'template' => '{create} {update} {delete}',
				'buttons' => [
					'create' => function ($url, $model, $key) {
						return Html::a('<span class="glyphicon glyphicon-plus"></span>', $url, [
							'title' => Yii::t('yii', 'create'),
							'data-pjax' => '0',
						]);
					},
				],
			],
        ],
    ]); ?>

</div>

<script>
	<?php $this->beginBlock('js_end') ?>

	$(function(){
		highlight_subnav("<?= Url::to(['category/index']) ?>");
	});

	<?php $this->endBlock() ?>
</script>

<?php $this->registerJs($this->blocks['js_end'], \yii\web\View::POS_END); ?>
